using System.Collections.Generic;
using System.Threading.Tasks;
using cmdb.keede.common;
using cmdb.keede.data;
using cmdb.keede.entity;
using System.Linq;
using Microsoft.EntityFrameworkCore;

namespace cmdb.keede.service
{
    public class BusinessInfoService : IBusinessInfoService
    {
        readonly IBusinessInfoRepository _businessRepository;
        readonly IUnitOfWork _unitOfWork;

        public BusinessInfoService(IBusinessInfoRepository businessRepository, IUnitOfWork unitOfWork)
        {
            _businessRepository = businessRepository;
            _unitOfWork = unitOfWork;
        }

        public async Task<BizResult<List<BusinessInfo>>> GetBusinessInfosAsync(int pageIndex, int pageSize)
        {
            var query = _businessRepository.GetQueryable().Where(w => w.Status == EnumBusinessStatus.Enabled).OrderBy(o => o.Id);
            var pageQuery = query.GetPage<BusinessInfo>(new Pager(pageIndex, pageSize));

            var total = await query.CountAsync();
            var results = await pageQuery.Include(b => b.Environment).ToListAsync();


            return new BizResult<List<BusinessInfo>>(results, new Pager(pageIndex, pageSize, total));
        }

        public async Task<BizResult<List<BusinessInfo>>> GetBusinessInfosAsync(int environmentInfoId)
        {
            var query = _businessRepository.GetQueryable().Where(w => w.Status == EnumBusinessStatus.Enabled && w.EnvironmentId == environmentInfoId).OrderBy(o => o.EnvironmentId);
            var results = await query.ToListAsync();

            return new BizResult<List<BusinessInfo>>(results);
        }

        public async Task<List<BusinessInfo>> GetAllBusinessInfosAsync()
        {
            return (await _businessRepository.GetManyAsync(w => w.Status == EnumBusinessStatus.Enabled)).ToList();
        }

        public async Task<BizResult<BusinessInfo>> GetBusinessInfoAsync(int id)
        {
            var obj = await _businessRepository.GetByIdAsync(id);
            return new BizResult<BusinessInfo>(obj);
        }

        public async Task<BizResult<bool>> DeleteBusinessInfoAsync(int id)
        {
            var obj = await _businessRepository.GetByIdAsync(id);
            if (obj != null)
            {
                obj.Status = EnumBusinessStatus.Disabled;
            }
            await _unitOfWork.CommitAsync();
            return new BizResult<bool>(true);
        }

        public async Task<BizResult<bool>> AddOrUpdateBusinessInfoAsync(BusinessInfo business)
        {
            if (business.Id == 0)
                _businessRepository.Add(business);
            else
            {
                var obj = await _businessRepository.GetByIdAsync(business.Id);
                obj.Name = business.Name;
                obj.Description = business.Description;
                obj.PICName = business.PICName;
                obj.EnvironmentId = business.EnvironmentId;
            }

            await _unitOfWork.CommitAsync();
            return new BizResult<bool>(true);
        }
    }
}